/*******************************************************************************
 * Copyright 2011 Mauro Luigi Drago (drago.luigi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.bytenose.extrautils.patterns.dispatcher;

import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.bytenose.extrautils.annotations.NonNullable;
import com.bytenose.extrautils.annotations.UtilityClass;

import com.google.common.base.*;

/**
 * A utility class defining filters for methods
 * based on their name.
 * @author Mauro Luigi Drago
 *
 */
@UtilityClass
public class NameFilters {
	
	/**
	 * Private constructor to avoid instantiation.
	 */
	private NameFilters() {
	}
	
	/**
	 * A filter matching methods with a specified name.
	 * @author Mauro Luigi Drago
	 *
	 */
	private static class SimpleNameFilter implements Predicate<Method> {
		/** The name of the method to catch */
		private final String methodName;
		
		/**
		 * Creates a new {@link SimpleNameFilter}.
		 * @param methodName 
		 * 		the name of the methods to filter, cannot be null.
		 * @throws IllegalArgumentException
		 * 		if some of the {@link NonNullable} arguments are null.
		 */
		private SimpleNameFilter(@NonNullable String methodName) 
			throws IllegalArgumentException {
			if (methodName == null) throw new IllegalArgumentException("methodName");
			this.methodName = methodName;
		}
		
		@Override
		public boolean apply(Method m) {
			return m.getName().equals(methodName);
		}
	}
	
	/**
	 * A filter matching methods with a name compatible
	 * with a specified regular expression.
	 * @author Mauro Luigi Drago
	 *
	 */
	private static class RegExNameFilter implements Predicate<Method> {
		/** The regular expression to math method names */
		private final Pattern namePattern;
		
		/**
		 * Creates a new {@link SimpleNameFilter}.
		 * @param namePattern 
		 * 		the regular expression to match method names, cannot be null.
		 * @throws IllegalArgumentException
		 * 		if some of the {@link NonNullable} arguments are null.
		 */
		private RegExNameFilter(@NonNullable Pattern namePattern) 
			throws IllegalArgumentException {
			if (namePattern == null) throw new IllegalArgumentException("namePattern");
			this.namePattern = namePattern;
		}
		
		@Override
		public boolean apply(Method m) {
			Matcher mt = namePattern.matcher(m.getName());
			return mt.matches();
		}
	}
	
	/**
	 * Creates a new filter matching methods with a specified name.
	 * @param methodName the name of the methods to filter, cannot be null.
	 * @return the filter.
	 * @throws IllegalArgumentException
	 * 		if some of the {@link NonNullable} arguments are null.
	 */
	@NonNullable
	public static Predicate<Method> getSimpleNameFilter(
			@NonNullable String methodName) 
			throws IllegalArgumentException {
		return new SimpleNameFilter(methodName);
	}
	
	/**
	 * Creates a new filter matching methods with name compliant to
	 * a specified regular expression.
	 * @param nameExpression the expression to match method names, cannot be null.
	 * @return the filter.
	 * @throws IllegalArgumentException
	 * 		if some of the {@link NonNullable} arguments are null.
	 * @throws PatternSyntaxException
	 * 		if {@code nameExpression} is not a valid regular expression. 
	 */
	@NonNullable
	public static Predicate<Method> getRegExpNameFilter(
			@NonNullable String nameExpression)
			throws IllegalArgumentException, PatternSyntaxException {
		if (nameExpression == null) throw new IllegalArgumentException("nameExpression");
		Pattern p = Pattern.compile(nameExpression);
		return new RegExNameFilter(p);
	}
}
